package com.ligenwen.zuoye.controller;


import com.ligenwen.zuoye.pojo.News;
import com.ligenwen.zuoye.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;


@Controller
public class UserController {

    @RequestMapping("getuser")
    @ResponseBody
    public User getUser() {
        User user = new User();
        user.setId(1);
        user.setAge(20);
        user.setName("user");
        return user;
    }

    @RequestMapping("login")
    public String getLogin(Model model){
        //int a = 0;
        //int b = 1 / a;

        //mybatis整合 1、单表增删改查->2、多表增删改查

        ArrayList<News> list = new ArrayList<>();
        for(int i=1;i<10;++i){
            News news = new News();
            news.id = i;
            news.title = "标题" + i;
            news.content = "内容" + i;
            list.add(news);
        }
        model.addAttribute("list",list);
        model.addAttribute("name","wang");
        return "login.html";
    }
    @RequestMapping("commit")
    public String commit(Model model,String username,String password){
        if(username.equals("wxj")&&password.equals("123456")){
            return "success.html";
        }else{
            return "fail.html";
        }
    }

    @RequestMapping("test")
    public String test(){
        return "test.html";
    }

    @RequestMapping("file")
    public String file(){
        return "file.html";
    }


    String uploadPath = "D:/data/";

    @RequestMapping("filecommit")
    public String filecommit(MultipartFile file){

        File file1 = new File(uploadPath);
        String name = UUID.randomUUID().toString() + file.getOriginalFilename();
        //file写到本地的绝对路径
        try {
            file.transferTo(new File(file1, name));
            return "success.html";
        } catch (IOException e) {
            //e.printStackTrace();
        }
        return "fail.html";
    }
}